﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SavannaFrame.Classes;

namespace SavannaFrame
{
    public partial class MarkerEdit : UserControl
    {
        Slot slot;
        object currentObject = null;
        SlotType currentType;
        ComboBox lastCombobox = null;
        Label labModifier = new Label();
        NumericUpDown nudModifier = new NumericUpDown();

        Stack<Control> stackComboBoxes = new Stack<Control>();
        Stack<SlotType> stackSlotTypes = new Stack<SlotType>();
        Stack<Object> stackContents = new Stack<object>();

        public MarkerEdit(Slot slot)
        {
            this.slot = slot;
            InitializeComponent();
        }

        public string GetMarker()
        {
            
            string marker = "";
            ComboBox cmb;// = stackComboBoxes.ElementAt<Control>();
            bool second=false;
            for (int i = stackComboBoxes.Count - 1; i >= 0; i--)
            {
                cmb = (ComboBox)stackComboBoxes.ElementAt(i);
                marker += "[" + cmb.Items[cmb.SelectedIndex].ToString().Trim().ToLower() + "].";
                if (!second)
                    second = true;
            }
            if (lastCombobox.SelectedItem.ToString().Trim().ToLower() != "#none")
            {
                marker += "[" + lastCombobox.SelectedItem.ToString() + "]";
                if (currentType == SlotType.Integer && nudModifier.Value != 0)
                    marker += ((nudModifier.Value > 0) ? "+" : "") + ((int)nudModifier.Value).ToString();
            }
            else
                marker = marker.Substring(0, marker.Length - 1);
            return marker;
        }

        private void addCombobox()
        {
            if (currentType == SlotType.Frame)
            {
                if (lastCombobox != null)
                {
                    stackComboBoxes.Push(lastCombobox);
                    lastCombobox.SelectedIndexChanged -= new EventHandler(lastCombobox_SelectedIndexChanged);
                    lastCombobox.Enabled = false;
                }
                lastCombobox = new ComboBox();
                lastCombobox.Dock = DockStyle.Top;
                this.SuspendLayout();
                this.Controls.Add(lastCombobox);
                this.ResumeLayout();
                List<Slot> slots = ((Frame)currentObject).getAllSlots();
                lastCombobox.Items.Add("#NONE");
                foreach (Slot slot in slots)
                    lastCombobox.Items.Add(slot.SlotName);
                lastCombobox.SelectedIndex = 0;

                lastCombobox.SelectedIndexChanged += new EventHandler(lastCombobox_SelectedIndexChanged);
            }
            else
                if (currentType == SlotType.Integer)
                {
                    this.Controls.Add(nudModifier);
                    this.Controls.Add(labModifier);
                }

        }

        void removeCombobox()
        {
            if (stackComboBoxes.Count > 0)
            {
                this.Controls.Remove(lastCombobox);
                lastCombobox.SelectedIndexChanged -= new EventHandler(lastCombobox_SelectedIndexChanged);

                lastCombobox = (ComboBox)this.stackComboBoxes.Pop();                
                currentObject = stackContents.Pop();
                currentType = stackSlotTypes.Pop();

                lastCombobox.SelectedIndexChanged += new EventHandler(lastCombobox_SelectedIndexChanged);
                lastCombobox.Enabled = true;

                this.Controls.Remove(nudModifier);
                this.Controls.Remove(labModifier);
            }
        }

        void lastCombobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox lastCombobox = sender as ComboBox;
            int ind = lastCombobox.SelectedIndex;
            if (ind != -1 && ind != 0)
            {
                Slot slot = ((Frame)currentObject).GetSlotByName(lastCombobox.Items[ind].ToString());
                currentType = slot.SlotType;
                if (slot.SlotType == SlotType.Frame)
                {
                    stackContents.Push(currentObject);
                    stackSlotTypes.Push(currentType);
                    currentObject = KnowLedgeBase.getFrameByID(slot.frameId);
                    currentType = SlotType.Frame;
                    addCombobox();
                }
                else
                    if (slot.SlotType == SlotType.Integer)
                        addCombobox();
                bRemoveLast.Enabled = stackComboBoxes.Count > 0;
            }
            else
            {
                this.Controls.Remove(nudModifier);
                this.Controls.Remove(labModifier);
            }
        }

        private void MarkerEdit_Load(object sender, EventArgs e)
        {
            currentType = SlotType.Frame;
            currentObject = KnowLedgeBase.getFrameByID(slot.ParentId);

            nudModifier.Value = 0;
            nudModifier.Dock = DockStyle.Top;
            nudModifier.Minimum = -100;
            nudModifier.Maximum = 100;

            this.labModifier.Text = "Модификатор: ";
            this.labModifier.Dock = DockStyle.Top;
            addCombobox();
        }

        private void bRemoveLast_Click(object sender, EventArgs e)
        {
            this.removeCombobox();
        }

        private void bTest_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.GetMarker());
        }
    }
}
